import { defineStore } from 'pinia'
//引入API
import { GetC1, GetC2, GetC3 } from '@/api/product/attr'
//引入类型
import { CategoryStore } from './types/type'

const useCategoryStore = defineStore('category', {
  state: (): CategoryStore => {
    return {
      category1Data: [],
      C1Id: '',
      category2Data: [],
      C2Id: '',
      category3Data: [],
      C3Id: '',
    }
  },
  actions: {
    //获取分类1数据
    async getCategory1() {
      const result = await GetC1()
      if (result.code == 200) {
        this.category1Data = result.data
      }
    },
    //获取分类2数据
    async getCategory2() {
      //清空c2和c3数据
      this.C2Id = ''
      this.C3Id = ''
      this.category3Data = []
      //获取c2数据
      const result = await GetC2(this.C1Id)
      if (result.code == 200) {
        this.category2Data = result.data
      }
    },
    //获取分类3数据
    async getCategory3() {
      //清空c3数据
      this.C3Id = ''
      //获取c3数据
      const result = await GetC3(this.C2Id)
      if (result.code == 200) {
        this.category3Data = result.data
      }
    },
  },
})
export default useCategoryStore
